package game.module.gm.processor;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.HallServer;
import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.club.logic.ClubManager;
import game.module.dabt.logic.Calculator;
import game.module.gm.logic.GmCmdManager;
import game.module.payment.logic.PaymentManager;
import game.module.turntables.processor.Login_turntableProcessor;
import game.module.user.bean.PlayerAddressBean;
import game.module.user.dao.PlayerDao;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageHall.C2SGmCmd;
import lua.LuaMessageHall.S2CGmCmd;

@MsgCodeAnn(msgcode = C2SGmCmd.id, accessLimit = 500)
public class GmCmdProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(GmCmdProcessor.class);

	@Override
	public void process(PlayingRole hero, RequestMessageRaw request) throws Exception {
		String cmd = request.readString();
		logger.info("playeyId={},cmd={}", hero.getId(), cmd);
		S2CGmCmd respMsg = new S2CGmCmd(0);
		if (!HallServer.getInstance().getServerConfig().isGmEnable()) {
			respMsg.ret = -1;
			hero.getGamePlayer().writeAndFlush(respMsg.build(hero.alloc()));
			return;
		}
		String[] cmdStrList = StringUtils.split(cmd);
		String cmdType = cmdStrList[0];
		switch (cmdType) {
		case "fangka":
			GmCmdManager.getInstance().fangKa(hero, cmdStrList, respMsg);
			break;
		case "jindou":
			GmCmdManager.getInstance().jindou(hero, cmdStrList, respMsg);
			break;
		case "item":
			GmCmdManager.getInstance().addItem(hero, cmdStrList, respMsg);
			break;
		case "mail":
			GmCmdManager.getInstance().addMail(hero, cmdStrList, respMsg);
			break;
		case "turntable1":
			Login_turntableProcessor.test(hero);
			break;
		case "clubname":
			System.out.println(ClubManager.getInstance().randomClubName());
			break;
		case "clubcode":
			ClubManager.getInstance().sendClubCode(hero.getId(), cmdStrList[1]);
			break;
		case "clubcreate":
			GmCmdManager.getInstance().createClub(hero, cmdStrList, respMsg);
			break;
		case "calculate":
			Calculator.getInstance().test();
			break;
		case "location":
			PlayerAddressBean bean = new PlayerAddressBean();
			bean.setAddress("IP");
			bean.setLat("35.620076");
			bean.setLng("105.067221");
			bean.setPlayerId(181);
			bean.setUpdateTime(new Date());
			PlayerDao.getInstance().insertOrUpdateAddress(bean);
			break;
		case "givememoney":
			String orderId = cmdStrList[1];
			int ptotal_fee = Integer.valueOf(cmdStrList[2]);
			PaymentManager.getInstance().swiftpassCallNotify(orderId, ptotal_fee);
			break;
		default:
			logger.error("unknown command={}", cmdType);
			respMsg.ret = -2;
			hero.getGamePlayer().writeAndFlush(respMsg.build(hero.alloc()));
		}
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// null
	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// null
	}

}
